package com.ls.fw.hadoop.client.util;

import java.util.ArrayList;
import java.util.List;

import net.sf.json.JSONArray;

import com.ls.fw.hadoop.client.bean.FileTreeItem;
import com.ls.fw.hadoop.client.bean.HadoopFile;


/**
 * 
 * <p></p>
 * @author ls 2013-9-2 下午01:25:27
 * @version V1.0   
 * @modificationHistory=========================逻辑或功能性重大变更记录
 * @modify by user: {修改人} 2013-9-2
 * @modify by reason:{方法名}:{原因}
 */
public class TreeUtil {
	 
	
	public static void main(String[] args) {
		List<FileTreeItem> list = null;
		list =new ArrayList<FileTreeItem>();
		//list = HDFSUtil.getRootDirectory();
		list.add(new FileTreeItem("test1","/test1",true));
		list.add(new FileTreeItem("test2","/test2",true));
		list.add(new FileTreeItem("test3","/test3",true));
       System.out.println( TreeUtil.getJSONArray(list).toString());		
	}
	
	/**
	 * 获取文件树的文件项
	 * @author ls 2013-9-2 下午01:31:59
	 * @param hadoopFile
	 * @return
	 */
	public static FileTreeItem getFileTreeItem(HadoopFile hadoopFile){
		return new FileTreeItem(hadoopFile.getName(),hadoopFile.getPath(),hadoopFile.isDir());
	}
	
	public static List<FileTreeItem> getFileTreeItem(List<HadoopFile> hadoopFile){
		 List<FileTreeItem> list= new ArrayList<FileTreeItem>();
		for (HadoopFile hadoopFile2 : hadoopFile) {
			list.add(TreeUtil.getFileTreeItem(hadoopFile2));
		}
		return list;
	}
	/**
	 * 返回一个JSONArray对象
	 * @author ls 2013-9-2 下午01:38:42
	 * @param hadoopFile
	 * @return
	 */
	/*public static JSONArray getJSONArray(List<HadoopFile> hadoopFile){
		  JSONArray jsonArray = JSONArray.fromObject( TreeUtil.getFileTreeItem(hadoopFile) );      
		  return jsonArray;
	}*/
	public static JSONArray getJSONArray(List<FileTreeItem> fileTreeItems){
		  JSONArray jsonArray = JSONArray.fromObject( fileTreeItems);      
		  return jsonArray;
	}
}
